package com.newedu.equipmgr.servlet.system.user;

import com.alibaba.fastjson.JSONObject;
import com.newedu.equipmgr.entity.SysUser;
import com.newedu.equipmgr.service.SysUserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(name="UserUpdateServlet",urlPatterns = {"/api/servlet/user/update"})
public class UserUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doPost(req, resp);

        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        String roleId = req.getParameter("role");


        SysUserService areaService = new SysUserService();
        boolean update = areaService.update(new SysUser(Integer.parseInt(id),username, pwd),roleId);
        JSONObject jsonObject = new JSONObject();
        if(update){//成功
            jsonObject.put("message","操作成功");
            jsonObject.put("status","success");

        }else{// 失败
            jsonObject.put("message","操作失败");
            jsonObject.put("status","fail");
        }

        resp.setContentType("application/json;charset=utf-8");
        String jsonString = jsonObject.toJSONString();
        System.out.println(jsonString);
        resp.getWriter().println(jsonString);
    }
}
